﻿using System.Reflection;

namespace Tszy.Unlimited.Base.Infrastructure.SlideCaptcha.Resources.Handler
{
    public class EmbeddedResourceHandler : IResourceHandler
    {
        public const string TYPE = "embedded";

        public bool CanHandle(string handlerType)
        {
            return handlerType == TYPE;
        }

        public byte[] Handle(SlideCaptchaResource resource)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var stream = assembly.GetManifestResourceStream(resource.Data);

            if (stream == null)
                return new byte[0];

            return StreamToBytes(stream);
        }

        private static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
    }
}
